﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSPL.DataAccess;
using System.Data;
using DSPL.Contracts.Common;
using System.Windows.Forms;

namespace DSPL.Services.Rules
{
  public  class SupplierRuleService
    {
      public bool CheckSupplierCreditLimit(int supplierPk,DateTime transactionDate,double debitAmount,int currencyPk,double rate)
      {
          var db = DatabaseAccess.Instance;
          List<ParameterWithValue> paramList = new List<ParameterWithValue>();
          bool isSuccess=true;
          paramList.Add(new ParameterWithValue("@supplierPk", supplierPk));
          paramList.Add(new ParameterWithValue("@transactionDate", transactionDate));
          paramList.Add(new ParameterWithValue("@totalAmount", debitAmount));
          paramList.Add(new ParameterWithValue("@currencyPk", currencyPk));
          paramList.Add(new ParameterWithValue("@rate", rate));
          paramList.Add(new ParameterWithValue("@baseCurrency",GlobalVariables.BaseCurrency ));          
          paramList.Add(new ParameterWithValue("@result", 1,System.Data.SqlDbType.Bit,System.Data.ParameterDirection.Output));
          paramList.Add(new ParameterWithValue("@message", "", System.Data.SqlDbType.VarChar, System.Data.ParameterDirection.Output,5000));

          SpCaller spCaller = new SpCaller() { SpName = "supplier.CHECK_CREDIT_LIMIT", SpParamValues = paramList.ToArray() };
          Dictionary<string, object> result = (Dictionary<string, object>)(db.ExcecuteMultipleOutputParameters(spCaller));

          foreach (KeyValuePair<string, object> outputParam in result)
          {
              if (outputParam.Key == "@result")
              {
                  if ((bool)outputParam.Value == true)
                  {
                      isSuccess = true;
                      break;
                  }
                  else
                  {
                      isSuccess =false;
                  }

              }
              if (outputParam.Key == "@message")
              {
                  MessageBox.Show(outputParam.Value.ToString(), "Stock Rules", MessageBoxButtons.OK, MessageBoxIcon.Information);

              }
             
          }

          return isSuccess;
            
      }
    }
}
